using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for TablesControl.
	/// </summary>
	public class TablesControl : AdminUserControl
	{
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private FreeFlowAdministrator.ExtendedDataGrid tablesGrid;
    private IContainer components;

		public TablesControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actions = new UIActionList();
      actions.Add("Refresh", "Refresh the list", new ActionExecuteDelegate(RefreshList));
      actions.Add("Copy to clipboard", "Copy the list of tables to the clipboard", 
        new ActionExecuteDelegate(CopyToClipboard));
      uiActionsControl.Actions = actions;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      this.tablesGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      ((System.ComponentModel.ISupportInitialize)(this.tablesGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(346, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 360);
      this.uiActionsControl.TabIndex = 0;
      // 
      // tablesGrid
      // 
      this.tablesGrid.AllowNavigation = false;
      this.tablesGrid.CaptionVisible = false;
      this.tablesGrid.DataMember = "";
      this.tablesGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.tablesGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.tablesGrid.Location = new System.Drawing.Point(0, 0);
      this.tablesGrid.Name = "tablesGrid";
      this.tablesGrid.Size = new System.Drawing.Size(346, 360);
      this.tablesGrid.TabIndex = 1;
      // 
      // TablesControl
      // 
      this.Controls.Add(this.tablesGrid);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "TablesControl";
      this.Size = new System.Drawing.Size(496, 360);
      ((System.ComponentModel.ISupportInitialize)(this.tablesGrid)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    private TableCollection tables;
    public void LoadTables(TableCollection tables)
    {
      this.tables = tables;
      tablesGrid.DataSource = null;
      tablesGrid.DataSource = tables;
      tablesGrid.ClearColumns();
      tablesGrid.AddColumn("Name");
      tablesGrid.AutosizeColumns();
    }

    private string CopyToClipboard()
    {
      tablesGrid.CopyToClipboard();
      return null;
    }

    private string RefreshList()
    {
      tables.Refresh();
      LoadTables(tables);
      OnRefreshRequired();
      return null;
    }
	}
}
